﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
using WpfHero;

namespace WpfHeroResponse
{
    /// <summary>
    /// Logic for song browsing
    /// </summary>
    public class SongBrowser
    {
        private readonly ObservableCollection<MyDirectory> subDirectories = new ObservableCollection<MyDirectory>();

        /// <summary>
        /// Gets the sub directories.
        /// </summary>
        /// <value>The sub directories.</value>
        public ObservableCollection<MyDirectory> SubDirectories
        {
            get { return subDirectories; }
        }

        private MyDirectory currentDirectory;

        /// <summary>
        /// Gets or sets the current directory.
        /// </summary>
        /// <value>The current directory.</value>
        public MyDirectory CurrentDirectory
        {
            get { return currentDirectory; }
            set 
            { 
                currentDirectory = value;
                subDirectories.Clear();
                if (Directory.GetDirectoryRoot(CurrentDirectory.FullPath) != CurrentDirectory.FullPath)
                    subDirectories.Add(new MyDirectory(Directory.GetParent(CurrentDirectory.FullPath).FullName, true));

                foreach (var dir in Directory.GetDirectories(CurrentDirectory.FullPath))
                    SubDirectories.Add(new MyDirectory(dir, false));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SongBrowser"/> class.
        /// </summary>
        public SongBrowser()
        {
            CurrentDirectory = new MyDirectory(Directory.GetCurrentDirectory(), false);
        }
    }



    /// <summary>
    /// Represents a directory that may have a song.
    /// </summary>
    public class MyDirectory
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MyDirectory"/> class.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        /// <param name="isParent">whether the dir is a parent dir</param>
        public MyDirectory(string fullPath, bool isParent)
        {
            FullPath = fullPath;
            IsParent = isParent;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name 
        {
            get { return IsParent ? ".." :  FullPath.Split(new char[] {'\\'}).Last(); }
        }

        /// <summary>
        /// Gets or sets the full path.
        /// </summary>
        /// <value>The full path.</value>
        public string FullPath { get; private set; }

        public bool IsParent { get; set; }

        /// <summary>
        /// Gets a value indicating whether this instance has song.
        /// </summary>
        /// <value><c>true</c> if this instance has song; otherwise, <c>false</c>.</value>
        public bool HasSong
        {
             get { return File.Exists(Utils.GetSongPath(FullPath));  }
        }
    }
}
